import scrapy
import json
import datetime
from scrapy import Request
from ..items import NewfkItem
import pymysql

# 父asin,二层数据
# https://www.4kmiles.com/api2/api/Product/GetProductDaySalesList?queryParam.accountId=5898&queryParam.site=us&queryParam.productId=1094445373&queryParam.productIds=1094445373&queryParam.stsType=1&queryParam.startDate=2021-07-06&queryParam.endDate=2021-07-12
list01 = []
list002 = []
list03 = []
# list04 = []
list20 = []
list21 = []
list22 = []
listshujv = []
listshujv1 = []
listshujv2 = []
listshujv3 = []
import time

py01 = False
py02 = False
py03 = False
py04 = False
today = datetime.date.today()
oneday1 = datetime.timedelta(days=1)
yesterday1 = today - oneday1
oneday2 = datetime.timedelta(days=32)
yesterday2 = today - oneday2
# 格式化成2016-03-20 11:45:39形式

newtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


class SikSpider(scrapy.Spider):
    name = 'fk'
    # allowed_domains = ['xxx']

    start_urls = ['https://www.baidu.com']

    # 总路径
    def parse(self, response, **kwargs):

        url = f'https://www.4kmiles.com/api2/api/SellerAccount/GetCurrentUserSellerAccountList'
        yield scrapy.Request(url=url, callback=self.linshi, meta={'url': url})

    def linshi(self, response):
        url = response.meta['url']
        # print(response.text)
        response = json.loads(response.text)
        # print(response)

        for i01 in response["result"]:

            id = i01["id"]
            sellerName = i01["sellerName"]
            print('*' * 30, sellerName)

            if sellerName != 'WSZCH':
                url01 = f'https://www.4kmiles.com/api2/api/Product/GetParentAsinPageList?queryParam.accountId={id}&pagination.rowsPerPage=10&pagination.currentPage=1&queryParam.IsListingView=true&queryParam.startDate={yesterday1}&queryParam.endDate={yesterday1}&queryParam.stockStatus=InStock&pagination.sortField=TodayOrders&pagination.sortType=desc'
                yield scrapy.Request(url=url01, callback=self.linshi02,
                                     meta={'id': id, 'url01': url01, 'yesterday': yesterday1, 'sellerName': sellerName})

    def linshi02(self, response):
        url01 = response.meta['url01']
        id = response.meta["id"]
        sellerName = response.meta["sellerName"]
        try:
            response1 = response.json()
            # print(id,response["result"]["totalCount"])
            num01 = response1["result"]["totalCount"]
            if num01 % 100 != 0:
                num02 = int(num01 / 100) + 1
                # print(id,'num',num02)

                for i in range(1, num02 + 1):
                    # url02 = f'https://www.4kmiles.com/api2/api/Product/GetParentAsinPageList?queryParam.accountId={id}&pagination.rowsPerPage=100&pagination.currentPage={i}&queryParam.IsListingView=true&queryParam.startDate={yesterday1}&queryParam.endDate={yesterday1}&queryParam.stockStatus=InStock&pagination.sortField=TodayOrders&pagination.sortType=desc'

                    url02 = f'https://www.4kmiles.com/api2/api/Product/GetParentAsinPageList?queryParam.accountId={id}&pagination.rowsPerPage=100&pagination.currentPage={i}&queryParam.IsListingView=true&queryParam.startDate={yesterday1}&queryParam.endDate={yesterday1}&queryParam.isFba=true&queryParam.productStatus=InStock&pagination.sortField=TodayOrders&pagination.sortType=desc'

                    yield scrapy.Request(url=url02, callback=self.one_parse,
                                         meta={'url02': url02, 'sellerName': sellerName})
            else:
                num02 = int(num01 / 100)

                for i in range(1, num02 + 1):
                    url02 = f'https://www.4kmiles.com/api2/api/Product/GetParentAsinPageList?queryParam.accountId={id}&pagination.rowsPerPage=100&pagination.currentPage={i}&queryParam.IsListingView=true&queryParam.startDate={yesterday1}&queryParam.endDate={yesterday1}&queryParam.isFba=true&queryParam.productStatus=InStock&pagination.sortField=TodayOrders&pagination.sortType=desc'

                    yield scrapy.Request(url=url02, callback=self.one_parse,
                                         meta={'url02': url02, 'sellerName': sellerName})
        except Exception as g:
            yield scrapy.Request(url=url01, callback=self.linshi02,
                                 meta={'url01': url01, 'id': id, 'sellerName': sellerName})

    # 父Asin
    def one_parse(self, response):
        # print('-'*30)
        url02 = response.meta["url02"]
        # print('0' * 30)
        sellerName = response.meta["sellerName"]
        # print('sellerName', sellerName)

        # try:
        response1 = response.json()
        zong = response1['result']['data']
        # print('zong', zong)
        for j in zong:
            # print(j)
            isFba = j["isFba"]
            if isFba == 1:
                isFba = 'FBA'
            elif isFba == 2:
                isFba = 'FBM'
            else:
                isFba = ''
            id = j["id"]
            asinId = j["asinId"]
            try:
                title = j["title"].replace("\n", "").replace("\r", "")
            except Exception as g:
                title = 'None'

            accountId = j['accountId']
            asin = j["asin"]
            list03.append(asin)
            sku = j["sku"]
            site = j["site"]
            productId = j["productId"]
            accountName = j['accountName']
            # 可售库存
            available = j['available']
            # 预留库存
            reserved = j['reserved']
            # 预留订单
            customerOrdersReservedQty = j['customerOrdersReservedQty']
            # 转运中
            reservedTransfersQty = j['reservedTransfersQty']
            # 处理中
            reservedProcessingQty = j['reservedProcessingQty']
            # 不可售库存
            unvailable = j['unvailable']
            # 图片
            try:
                imagePath = j['imagePath']
            except Exception as g:
                imagePath = 'None'
            # 分组
            productGroup = j['productGroup']
            # listing评分1
            reviewScore = j['reviewScore']
            # listing评分2
            reviewCount = j['reviewCount']
            # 在途库存已创建
            workInbound = j['workInbound']
            # 在途库存已发货
            shipInbound = j['shipInbound']
            # 在途库存接受中
            inbound = j['inbound']

            fa_guolv = asin + sku + site + sellerName + str(yesterday1)
            fa_guolv = fa_guolv.replace(' ', '')
            # print('fa_goulv',fa_guolv)
            if fa_guolv not in list22:
                list22.append(fa_guolv)

                # if len(list22) == 1:
                #     print('执行数据库')
                #
                #     database = "sunway_db"
                #     # 连接database
                #     conn = pymysql.connect(host="192.168.2.205", user="jinhuitao", password="jinhuitao_123456",
                #                            database=database, charset="utf8", port=3306)
                #     cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
                #     cursor.execute("SELECT guolv FROM father_asin")
                #     # 获取全部匹配数据
                #     student_info = cursor.fetchall()
                #
                #     # 关闭数据库
                #     conn.close()
                #     g1=open('./jim01.csv', 'w', encoding='utf-8')
                #     for i1 in student_info:
                #         # print(i1)
                #         listshujv.append(i1['guolv'].replace(' ','').lower())  # 将从数据库中获取的数据存储到self.cache_list6
                #         g1.write(f"{i1['guolv']}\n")
                #     # print('listshujv', listshujv)
                #
                #
                #
                #     database1 = "sunway_db"
                #     # 连接database
                #     conn1 = pymysql.connect(host="192.168.2.205", user="jinhuitao", password="jinhuitao_123456",
                #                             database=database, charset="utf8", port=3306)
                #     cursor1 = conn1.cursor(cursor=pymysql.cursors.DictCursor)
                #     cursor1.execute("SELECT son_zhu FROM son_asin")
                #     # 获取全部匹配数据
                #     student_info1 = cursor1.fetchall()
                #     # 关闭数据库
                #     conn1.close()
                #     g2=open('./jim02.csv', 'w', encoding='utf-8')
                #     for i11 in student_info1:
                #         listshujv1.append(i11['son_zhu'].replace(' ','').lower())  # 将从数据库中获取的数据存储到self.cache_list
                #     # print('listshujv1', listshujv1)
                #
                #         g2.write(f"{i11['son_zhu']}\n")
                #
                #     database2 = "sunway_db"
                #     # 连接database
                #     conn2 = pymysql.connect(host="192.168.2.205", user="jinhuitao", password="jinhuitao_123456",
                #                             database=database, charset="utf8", port=3306)
                #     cursor2 = conn2.cursor(cursor=pymysql.cursors.DictCursor)
                #     cursor2.execute("SELECT fa_guolv FROM stock_fa")
                #     # 获取全部匹配数据
                #     student_info2 = cursor2.fetchall()
                #     # 关闭数据库
                #     conn2.close()
                #     g3 = open('./jim03.csv', 'w', encoding='utf-8')
                #     for i111 in student_info2:
                #         listshujv2.append(i111['fa_guolv'].replace(' ','').lower())  # 将从数据库中获取的数据存储到self.cache_list
                #     # print('listshujv2', listshujv2)
                #
                #         g3.write(f"{i111['fa_guolv']}\n")
                #
                #     database3 = "sunway_db"
                #     # 连接database
                #     conn3 = pymysql.connect(host="192.168.2.205", user="jinhuitao", password="jinhuitao_123456",
                #                             database=database, charset="utf8", port=3306)
                #     cursor3 = conn3.cursor(cursor=pymysql.cursors.DictCursor)
                #     cursor3.execute("SELECT son_guolv FROM stock_son")
                #     # 获取全部匹配数据
                #     student_info3 = cursor3.fetchall()
                #     # 关闭数据库
                #     conn3.close()
                #     g4=open('./jim04.csv', 'w', encoding='utf-8')
                #     for i1111 in student_info3:
                #         listshujv3.append(i1111['son_guolv'].replace(' ','').lower())  # 将从数据库中获取的数据存储到self.cache_list
                #     # print('listshujv3', listshujv3)
                #
                #         g4.write(f"{i1111['son_guolv']}\n")
                # py01='0'
                # global py01
                # if fa_guolv.replace(' ','').lower() not in listshujv2:
                #     py01 = True

                buchong = NewfkItem()
                buchong['py01'] = py01
                buchong['fa_guolv'] = fa_guolv

                # buchong['listshujv2'] = listshujv2
                buchong['yesterday1'] = yesterday1
                buchong['sku1'] = sku
                buchong['asin1'] = asin
                buchong['accountName1'] = accountName
                buchong['site1'] = site
                buchong['newtime'] = newtime
                buchong['available'] = available
                buchong['reserved'] = reserved
                buchong['customerOrdersReservedQty'] = customerOrdersReservedQty
                buchong['reservedTransfersQty'] = reservedTransfersQty
                buchong['reservedProcessingQty'] = reservedProcessingQty
                buchong['unvailable'] = unvailable
                buchong['imagePath'] = imagePath
                buchong['productGroup'] = productGroup
                buchong['reviewScore'] = reviewScore
                buchong['reviewCount'] = reviewCount
                buchong['workInbound'] = workInbound
                buchong['shipInbound'] = shipInbound
                buchong['inbound'] = inbound
                yield buchong

                if productId == ' ':
                    yield Request(url=url02, callback=self.one_parse, meta={'url02': url02})
                else:
                    # 子ASIN接口
                    ull = f'https://www.4kmiles.com/api2/api/Product/GetProductPageList?pagination.rowsPerPage=499&pagination.currentPage=1&queryParam.accountId={accountId}&queryParam.site={site}&queryParam.parentAsin={asin}&queryParam.startDate={yesterday1}&queryParam.endDate={yesterday1}&queryParam.stockStatus=InStock'

                    yield Request(url=ull, callback=self.three_parse,
                                  meta={'id1': id, 'asin': asin, 'sku': sku, 'asinId': asinId, 'ull': ull,
                                        'accountId': accountId, 'site': site, 'productId': productId,
                                        'isFba': isFba, 'title': title, 'accountName': accountName,
                                        'sellerName': sellerName})
            else:
                continue
        # except Exception as g:
        #     # print('8' * 30)
        #     yield scrapy.Request(url=url02, callback=self.one_parse, meta={'url02': url02,'sellerName':sellerName})

    # 子ASIN
    def three_parse(self, response):

        id1 = response.meta["id1"]
        asin1 = response.meta["asin"]
        sku1 = response.meta["sku"]
        asinId1 = response.meta["asinId"]
        ull1 = response.meta["ull"]
        accountId1 = response.meta["accountId"]
        site1 = response.meta["site"]
        productId1 = response.meta["productId"]
        isFba1 = response.meta["isFba"]
        title1 = response.meta["title"]
        accountName = response.meta["accountName"]
        sellerName = response.meta["sellerName"]
        faandzi1 = asin1 + sku1 + site1
        res1 = json.loads(response.text)
        zong1 = res1['result']['data']
        list_zi = []
        list02 = []
        list04 = []
        list05 = []
        list06 = []
        list07 = []
        list08 = []
        list09 = []
        list10 = []
        list_guolv = []
        for jj in zong1:

            isFba2 = jj["isFba"]
            if isFba2 == 1:
                isFba2 = 'FBA'
            elif isFba2 == 2:
                isFba2 = 'FBM'
            else:
                isFba2 = ''
            list08.append(isFba2)
            try:
                title2 = jj["title"].replace("\n", "").replace("\r", "")
            except Exception as g:
                title2 = 'None'
            list05.append(title2)
            asin2 = jj["asin"]
            list02.append(asin2)
            sku2 = jj["sku"]
            list06.append(sku2)
            asinid2 = jj['asinId']
            list04.append(asinid2)
            site2 = jj["site"]
            list07.append(site2)
            id2 = jj["id"]
            list_zi.append(id2)
            accountName2 = jj["accountName"]
            list09.append(accountName2)
            # 可售库存
            available2 = jj['available']
            # 预留库存
            reserved2 = jj['reserved']
            # 预留订单
            customerOrdersReservedQty2 = jj['customerOrdersReservedQty']
            # 转运中
            reservedTransfersQty2 = jj['reservedTransfersQty']
            # 处理中
            reservedProcessingQty2 = jj['reservedProcessingQty']
            # 不可售库存
            unvailable2 = jj['unvailable']
            # 图片
            try:
                imagePath2 = jj['imagePath']
            except Exception as g:
                imagePath2 = 'None'
            # 分组
            productGroup2 = jj['productGroup']
            # listing评分1
            reviewScore2 = jj['reviewScore']
            # listing评分2
            reviewCount2 = jj['reviewCount']
            # 在途库存已创建
            workInbound2 = jj['workInbound']
            # 在途库存已创建
            shipInbound2 = jj['shipInbound']
            # 在途库存接受中
            inbound2 = jj['inbound']
            # 父子ASIN桥接字段
            fa_son = asin1 + site1 + sellerName
            list10.append(fa_son)
            # 子ASIN重复判断过滤
            son_guolv = asin2 + sku2 + site2 + sellerName + str(yesterday1)
            son_guolv = son_guolv.replace(' ', '')
            faandzi2 = asin2 + sku2 + site2
            list_guolv.append(faandzi2)
            # print('*'*20)
            # print(faandzi1)
            # print(faandzi2)
            # print('&'*20)
            # print('son_guolv',son_guolv)
            if son_guolv not in list20 and faandzi2 != faandzi1:
                list20.append(son_guolv)
                # print('jim')
                # py04='0'
                # global py04
                # if son_guolv.replace(' ','').lower() not in listshujv3:
                #     py04 = True
                buchong2 = NewfkItem()
                buchong2["py04"] = py04
                buchong2['son_guolv'] = son_guolv
                # buchong2['listshujv3'] = listshujv3
                buchong2['father_asin'] = asin1
                buchong2['yesterday1'] = yesterday1
                buchong2['sku2'] = sku2
                buchong2['asin2'] = asin2
                buchong2['accountName2'] = accountName2
                buchong2['site2'] = site2
                buchong2['newtime'] = newtime
                buchong2['available2'] = available2
                buchong2['reserved2'] = reserved2
                buchong2['customerOrdersReservedQty2'] = customerOrdersReservedQty2
                buchong2['reservedTransfersQty2'] = reservedTransfersQty2
                buchong2['reservedProcessingQty2'] = reservedProcessingQty2
                buchong2['unvailable2'] = unvailable2
                buchong2['imagePath2'] = imagePath2
                buchong2['productGroup2'] = productGroup2
                buchong2['reviewScore2'] = reviewScore2
                buchong2['reviewCount2'] = reviewCount2
                buchong2['workInbound2'] = workInbound2
                buchong2['shipInbound2'] = shipInbound2
                buchong2['inbound2'] = inbound2
                yield buchong2



            else:
                continue
        list_zi_str = str(list_zi)[1:-1].replace(' ', '')
        print('1' * 10, list_zi_str)

        # 拼接父ASIN详情页面接口
        if len(list_zi_str) != 0:

            url_fa = f'https://www.4kmiles.com/api2/api/Product/GetProductDayDetailList?queryParam.accountId={accountId1}&queryParam.site={site1}&queryParam.productId={productId1}&queryParam.stsType=1&queryParam.startDate={yesterday2}&queryParam.endDate={yesterday1}&queryParam.asinId={asinId1}&queryParam.calcType=1&queryParam.productIds={list_zi_str}'
            print('url_fa' * 10, url_fa)
            yield scrapy.Request(url=url_fa, callback=self.fa_xiang,
                                 meta={'site1': site1, 'id1': id1, 'asin': asin1, 'sku': sku1, 'asinId': asinId1,
                                       'site': site1, 'accountId': accountId1, 'isFba': isFba1, 'title': title1,
                                       'accountName': accountName, 'url_fa': url_fa, 'sellerName': sellerName})
        else:
            url_fa = f'https://www.4kmiles.com/api2/api/Product/GetProductDayDetailList?queryParam.accountId={accountId1}&queryParam.site={site1}&queryParam.productId={productId1}&queryParam.stsType=1&queryParam.startDate={yesterday2}&queryParam.endDate={yesterday1}&queryParam.asinId={asinId1}&queryParam.calcType=1&queryParam.productIds={productId1}'
            # print('url_fa' * 20, url_fa)
            yield scrapy.Request(url=url_fa, callback=self.fa_xiang,
                                 meta={'site1': site1, 'id1': id1, 'asin': asin1, 'sku': sku1, 'asinId': asinId1,
                                       'site': site1,
                                       'accountId': accountId1, 'isFba': isFba1, 'title': title1,
                                       'accountName': accountName, 'url_fa': url_fa, 'sellerName': sellerName})

        # 拼接子ASIN详情页面接口
        for i in range(len(list02)):

            if list_guolv[i] != faandzi1:  # 判断子asin数据与父asin数据相同list_guolv是每个父asin下子asin数据的列表集
                url_son = f'https://www.4kmiles.com/api2/api/Product/GetProductDayDetailList?queryParam.accountId={accountId1}&queryParam.site={site1}&queryParam.productId={list_zi[i]}&queryParam.stsType=1&queryParam.startDate={yesterday2}&queryParam.endDate={yesterday1}&queryParam.asinId={list04[i]}&queryParam.calcType=1&queryParam.productIds={list_zi[i]}'
                yield scrapy.Request(url=url_son, callback=self.son_xiang,
                                     meta={'url_son': url_son, 'title2': list05[i], 'asin2': list02[i],
                                           'sku2': list06[i], 'site2': list07[i], 'id2': list_zi[i],
                                           'isFba2': list08[i], 'asinId2': list04[i], 'accountName2': list09[i],
                                           'fa_son': list10[i], 'father_asin2': asin1, 'sellerName': sellerName})
            else:
                continue

    # 父ASIN详情页面
    def fa_xiang(self, response):

        sellerName = response.meta['sellerName']
        url_fa = response.meta['url_fa']
        site1 = response.meta['site1']
        id1 = response.meta['id1']
        sku1 = response.meta['sku']
        asin1 = response.meta['asin']
        asinId1 = response.meta['asinId']
        isFba1 = response.meta['isFba']
        title1 = response.meta['title']
        accountName = response.meta['accountName']

        fa_zong = response.json()
        try:
            if fa_zong['success'] == True:
                for j in fa_zong['result']:

                    # 日期
                    belongDate = j['belongDate']
                    # 价格
                    price = j['price']
                    # 大类排名
                    bsrRank = j['bsrRank']
                    # 大类目
                    bsrCategory = j['bsrCategory']
                    # 小类排名
                    bsrRankLs = j['bsrRankLs']
                    # 小类目
                    bsrCategoryLs = j['bsrCategoryLs']
                    # 总流量1
                    adTraffic = j['adTraffic']

                    # 总流量3
                    review = j['review']
                    # 总流量4
                    sessions = j['sessions']
                    # 总流量5
                    traffic = j['traffic']
                    # 自然流量
                    naturalTraffic = j['naturalTraffic']
                    ucr = j['ucr']
                    ocr = j['ocr']
                    # 自然流量转化率
                    naturalCR = j['naturalCR']
                    # 订单量
                    orders = j['orders']
                    # 广告订单量
                    adOrders = j['adOrders']
                    # 销量
                    quantities = j['quantities']
                    # 销售额
                    sales = j['sales']
                    # 总收入
                    totalIncome = j['totalIncome']
                    # 总费用
                    costs = j['costs']
                    # 毛利
                    profits = j['profits']
                    # 毛利率
                    profitsRate = j['profitsRate']
                    # 亚马逊物流费
                    fbaFee = j['fbaFee']
                    # 亚马逊雇佣金
                    salesCommission = j['salesCommission']
                    # 促销折扣
                    promotionFee = j['promotionFee']
                    # 采购成本
                    unitCost = j['unitCost']
                    # FBA头程费用
                    fbaFeeRate = j['fbaFeeRate']
                    # FBM配送费用
                    fbmShippingCostRate = j['fbmShippingCostRate']
                    # 退款管理费用
                    refundManageFee = j['refundManageFee']
                    # 退货数量
                    returnOrders = j['returnOrders']
                    # 退货率
                    returnRate = j['returnRate']
                    # 广告曝光量
                    impressions = j['impressions']
                    # 广告点击量
                    clicks = j['clicks']
                    ctr = j['ctr']
                    # 广告转化率
                    adCR = j['adCR']
                    # 广告销售额goodsSale
                    adSales = j['adSales']
                    # 广告花费
                    spend = j['spend']
                    aCoS = j['aCoS']
                    Roas = j['adRoi']
                    acoAs = j['acoAs']
                    asoAs = j['asoAs']
                    # 留评率
                    reviewRate = j['reviewRate']
                    guolv = asin1 + sku1 + site1 + str(belongDate) + sellerName
                    guolv = guolv.replace(' ', '')

                    if guolv not in list01:
                        # list21.append(guolv)
                        item = NewfkItem()
                        # py02='0'
                        # global py02
                        # if guolv.replace(' ','').lower() not in listshujv:
                        #     py02 = True
                        # item['listshujv'] = listshujv
                        item["py02"] = py02
                        item['guolv'] = guolv
                        item['site1'] = site1
                        item['id1'] = id1
                        item['sku1'] = sku1
                        item['asin1'] = asin1
                        item['asinId1'] = asinId1
                        item['isFba1'] = isFba1
                        item['title1'] = title1
                        item['newtime'] = newtime
                        item['belongDate'] = belongDate
                        item['price'] = price
                        item['bsrRank'] = bsrRank
                        item['bsrCategory'] = bsrCategory
                        item['bsrRankLs'] = bsrRankLs
                        item['bsrCategoryLs'] = bsrCategoryLs
                        item['adTraffic'] = adTraffic
                        item['review'] = review
                        item['sessions'] = sessions
                        item['traffic'] = traffic
                        item['naturalTraffic'] = naturalTraffic
                        item['ucr'] = ucr
                        item['ocr'] = ocr
                        item['naturalCR'] = naturalCR
                        item['orders'] = orders
                        item['adOrders'] = adOrders
                        item['quantities'] = quantities
                        item['goodsSale'] = sales
                        item['totalIncome'] = totalIncome
                        item['costs'] = costs
                        item['profits'] = profits
                        item['profitsRate'] = profitsRate
                        item['fbaFee'] = fbaFee
                        item['salesCommission'] = salesCommission
                        item['promotionFee'] = promotionFee
                        item['unitCost'] = unitCost
                        item['fbaFeeRate'] = fbaFeeRate
                        item['fbmShippingCostRate'] = fbmShippingCostRate

                        item['refundManageFee'] = refundManageFee
                        item['returnOrders'] = returnOrders
                        item['returnRate'] = returnRate
                        item['impressions'] = impressions
                        item['clicks'] = clicks
                        item['ctr'] = ctr
                        item['adCR'] = adCR
                        item['adSales'] = adSales
                        item['spend'] = spend
                        item['aCoS'] = aCoS
                        item['Roas'] = Roas
                        item['acoAs'] = acoAs
                        item['asoAs'] = asoAs
                        item['reviewRate'] = reviewRate
                        item['accountName'] = accountName
                        list01.append(guolv)
                        yield item
            elif fa_zong['success'] == False:
                jim = NewfkItem()
                jim['newtime'] = newtime
                jim['url_fa'] = url_fa
                jim['asin1'] = asin1
                yield jim
        except Exception as g:
            print('*' * 10, '父', fa_zong)

    # 子ASIN详情页面
    def son_xiang(self, response):

        sellerName = response.meta['sellerName']
        father_asin2 = response.meta['father_asin2']
        asinId2 = response.meta['asinId2']
        title2 = response.meta['title2']
        asin2 = response.meta['asin2']
        sku2 = response.meta['sku2']
        site2 = response.meta['site2']
        id2 = response.meta['id2']
        isFba2 = response.meta['isFba2']
        accountName2 = response.meta['accountName2']
        url_son = response.meta['url_son']
        fa_son = response.meta['fa_son']
        son_zong = response.json()
        print('000' * 20)
        # print(son_zong)
        try:
            if son_zong['success'] == True:
                for j in son_zong['result']:

                    # 日期
                    belongDate2 = j['belongDate']
                    # 价格
                    price2 = j['price']
                    # 大类排名
                    bsrRank2 = j['bsrRank']
                    # 大类目
                    bsrCategory2 = j['bsrCategory']
                    # 小类排名
                    bsrRankLs2 = j['bsrRankLs']
                    # 小类目
                    bsrCategoryLs2 = j['bsrCategoryLs']
                    # 总流量1
                    adTraffic2 = j['adTraffic']

                    # 总流量3
                    review2 = j['review']
                    # 总流量4
                    sessions2 = j['sessions']
                    # 总流量5
                    traffic2 = j['traffic']
                    # 自然流量
                    naturalTraffic2 = j['naturalTraffic']
                    ucr2 = j['ucr']
                    ocr2 = j['ocr']
                    # 自然流量转化率
                    naturalCR2 = j['naturalCR']
                    # 订单量
                    orders2 = j['orders']
                    # 广告订单量
                    adOrders2 = j['adOrders']
                    # 销量
                    quantities2 = j['quantities']
                    # 销售额
                    sales2 = j['sales']
                    # 总收入
                    totalIncome2 = j['totalIncome']
                    # 总费用
                    costs2 = j['costs']
                    # 毛利
                    profits2 = j['profits']
                    # 毛利率
                    profitsRate2 = j['profitsRate']
                    # 亚马逊物流费
                    fbaFee2 = j['fbaFee']
                    # 亚马逊雇佣金
                    salesCommission2 = j['salesCommission']
                    # 促销折扣
                    promotionFee2 = j['promotionFee']
                    # 采购成本
                    unitCost2 = j['unitCost']
                    # FBA头程费用
                    fbaFeeRate2 = j['fbaFeeRate']
                    # FBM配送费用
                    fbmShippingCostRate2 = j['fbmShippingCostRate']
                    # 退款管理费用
                    refundManageFee2 = j['refundManageFee']
                    # 退货数量
                    returnOrders2 = j['returnOrders']
                    # 退货率
                    returnRate2 = j['returnRate']
                    # 广告曝光量
                    impressions2 = j['impressions']
                    # 广告点击量
                    clicks2 = j['clicks']
                    ctr2 = j['ctr']
                    # 广告转化率
                    adCR2 = j['adCR']
                    # 广告销售额
                    adSales2 = j['adSales']
                    # 广告花费
                    spend2 = j['spend']
                    aCoS2 = j['aCoS']
                    Roas2 = j['adRoi']
                    acoAs2 = j['acoAs']
                    asoAs2 = j['asoAs']
                    # 留评率
                    reviewRate2 = j['reviewRate']
                    # 子ASIN主键
                    son_zhu = asin2 + sku2 + site2 + str(belongDate2) + sellerName
                    son_zhu = son_zhu.replace(' ', '')
                    if son_zhu not in list002:
                        list002.append(son_zhu)
                        # py03='0'
                        # global py03
                        # if son_zhu.replace(' ','').lower() not in listshujv1:
                        #     py03 = True
                        item = NewfkItem()
                        item["py03"] = py03
                        # item['listshujv1'] = listshujv1

                        # 主键
                        item['son_zhu'] = son_zhu
                        item['site2'] = site2
                        item['id2'] = id2
                        item['sku2'] = sku2
                        item['asin2'] = asin2
                        item['asinId2'] = asinId2
                        item['isFba2'] = isFba2
                        item['title2'] = title2
                        item['newtime'] = newtime
                        item['belongDate2'] = belongDate2
                        item['price2'] = price2
                        item['bsrRank2'] = bsrRank2
                        item['bsrCategory2'] = bsrCategory2
                        item['bsrRankLs2'] = bsrRankLs2
                        item['bsrCategoryLs2'] = bsrCategoryLs2
                        item['adTraffic2'] = adTraffic2
                        item['review2'] = review2
                        item['sessions2'] = sessions2
                        item['traffic2'] = traffic2
                        item['naturalTraffic2'] = naturalTraffic2
                        item['ucr2'] = ucr2
                        item['ocr2'] = ocr2
                        item['naturalCR2'] = naturalCR2
                        item['orders2'] = orders2
                        item['adOrders2'] = adOrders2
                        item['quantities2'] = quantities2
                        item['goodsSale2'] = sales2
                        item['totalIncome2'] = totalIncome2
                        item['costs2'] = costs2
                        item['profits2'] = profits2
                        item['profitsRate2'] = profitsRate2
                        item['fbaFee2'] = fbaFee2
                        item['salesCommission2'] = salesCommission2
                        item['promotionFee2'] = promotionFee2
                        item['unitCost2'] = unitCost2
                        item['fbaFeeRate2'] = fbaFeeRate2
                        item['fbmShippingCostRate2'] = fbmShippingCostRate2

                        item['refundManageFee2'] = refundManageFee2
                        item['returnOrders2'] = returnOrders2
                        item['returnRate2'] = returnRate2
                        item['impressions2'] = impressions2
                        item['clicks2'] = clicks2
                        item['ctr2'] = ctr2
                        item['adCR2'] = adCR2
                        item['adSales2'] = adSales2
                        item['spend2'] = spend2
                        item['aCoS2'] = aCoS2
                        item['Roas2'] = Roas2
                        item['acoAs2'] = acoAs2
                        item['asoAs2'] = asoAs2
                        item['reviewRate2'] = reviewRate2
                        item['accountName2'] = accountName2
                        # 外键
                        item['fa_son'] = fa_son + belongDate2

                        item['father_asin2'] = father_asin2

                        yield item
                    else:
                        continue
            elif son_zong['success'] == False:
                jim = NewfkItem()
                jim['newtime'] = newtime
                jim['url_son'] = url_son
                jim['asin2'] = asin2
                yield jim
        except Exception as g:
            print('*' * 10, '子', son_zong)
